/**
 * Copyright (c) 2016-2100 yunclouds
 */
package com.yunqian.redot.service;

import com.yunqian.exception.BaseException;
import com.yunqian.exception.ServiceException;
import com.yunqian.redot.constant.CacheNames;
import com.yunqian.redot.domain.Users;
import com.yunqian.redot.repository.UsersRepository;
import com.yunqian.service.BaseService;
import com.yunqian.util.validate.Validator;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Map;


/**
 *
 *
 * @author zsy
 * 2018-09-29 15:18:12
 */
@Service
@CacheConfig(cacheNames = CacheNames.USERS)
@Log4j2
public class UsersService extends BaseService<Users, Long> {

    @Autowired
    private UsersRepository usersRepository;

    @Override
    public Page<Users> pagination(Map<String, Object> params) {
        return super.pagination(params);
    }

    @Cacheable(key = "#p0")
    @Override
    public Users findStrictOne(Long id) {
        return super.findStrictOne(id);
    }

    /**
     * 逻辑删除  //状态 1-正常 2-删除
     *
     * @param id
     */
    @CacheEvict(key = "#p0")
    @Override
    public void delete(Long id) {
        Users user = this.findStrictOne(id);
        super.update(user);
    }

    /**
     * 根据用户名称查找用户
     *
     * @param name
     * @return
     */
    public Users findByName(String name) {
        return this.usersRepository.findByName(name);
    }


    @Override
    public Users findOne(Long id) {
        return super.findOne(id);
    }

    @Override
    public Users save(Users users) {
        Users u = this.findByName(users.getName());
        if(Validator.isNotNull(u)){
            throw new ServiceException("添加失败，该账号名已存在");
        }
        users.setPassword(DigestUtils.md5DigestAsHex(users.getPassword().getBytes()));
        return super.save(users);
    }

    @Override
    public Users update(Long id, Map<String, Object> map) {
        String password = (String) map.get("password");
        if(Validator.isNotNull(password)){
            map.put("password", DigestUtils.md5DigestAsHex(password.getBytes()));
        }
        return super.update(id, map);
    }

    /**
     * 用户修改自己的密码
     *
     * @param userId
     * @param map
     * @return
     */
    public Users updatePassword(Long userId, Map<String, Object> map) {
        Users user = this.findStrictOne(userId);
        if(!user.getPassword().equals(DigestUtils.md5DigestAsHex((map.get("old_password").toString()).getBytes()))){
            throw new BaseException("旧密码不正确");
        }
        user.setPassword(DigestUtils.md5DigestAsHex((map.get("password").toString()).getBytes()));
        user = this.update(user);
        return user;
    }

}
